package com.sxpi.zixun.service.coursecomment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.CourseCommentBo;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.ex.PugValidatorException;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.CourseCommentMapper;
import com.sxpi.zixun.pojo.CourseComment;
import com.sxpi.zixun.vo.CourseCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/5 20:03
 */
@Service
@Slf4j
public class ICourseCommentServiceServiceImpl extends ServiceImpl<CourseCommentMapper, CourseComment> implements ICourseCommentService {

    /**
     * 查询课程评论列表信息
     * @return
     */
    @Override
    public List<CourseCommentBo> findCourseCommentList() {
        LambdaQueryWrapper<CourseComment> courseCommentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<CourseComment> list = this.list();
        return tranferListBo(list,CourseCommentBo.class);
    }

    /**
     * 查询课程评论列表信息并分页
     * @param coursecommentVo
     * @return
     */
    @Override
    public IPage<CourseCommentBo> findCourseCommentPage(CourseCommentVo coursecommentVo) {
        Page<CourseComment> courseCommentPage = new Page<>(coursecommentVo.getPageNo(),coursecommentVo.getPageSize());
        LambdaQueryWrapper<CourseComment> courseCommentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 排除description字段
        courseCommentLambdaQueryWrapper.select(CourseComment.class,colum -> !colum.getColumn().equals("description"));
        courseCommentLambdaQueryWrapper.and(Vsserts.isNotEmpty(coursecommentVo.getKeyword()),wrapper -> wrapper
                .like(CourseComment::getNickname,coursecommentVo.getKeyword())
                .or()
                .like(CourseComment::getContent,coursecommentVo.getKeyword())
                .or()
                .like(CourseComment::getCoursecatename,coursecommentVo.getKeyword())
                .or()
                .like(CourseComment::getReplyusernickname,coursecommentVo.getKeyword()));
        // 根据时间排降序
        courseCommentLambdaQueryWrapper.orderByDesc(CourseComment::getCreateTime);
        Page<CourseComment> page = this.page(courseCommentPage, courseCommentLambdaQueryWrapper);
        return tranferPageBo(page,CourseCommentBo.class);
    }

    /**
     * 根据id查询课程评论明细信息
     * @param id
     * @return
     */
    @Override
    public CourseCommentBo getCourseCommentById(Long id) {
        CourseComment courseComment = this.getById(id);
        return tranferBo(courseComment,CourseCommentBo.class);
    }

    /**
     * 保存&修改课程评论
     * @param coursecomment
     * @return
     */
    @Override
    public CourseCommentBo saveUpdateCourseComment(CourseComment coursecomment) {
        boolean flag = this.saveOrUpdate(coursecomment);
        return flag ? tranferBo(coursecomment,CourseCommentBo.class)  : null;
    }

    /**
     * 根据id删除课程评论
     * @param id
     * @return
     */
    @Override
    public int deleteCourseCommentById(Long id) {
        CourseComment courseComment = this.getById(id);
        if(courseComment != null){
            CourseComment courseCommentNew = new CourseComment();
            courseCommentNew.setId(id);
            this.removeById(courseCommentNew);
            return 1;
        }
        return 0;
    }

    /**
     * 根据课程评论ids批量删除课程评论
     * @param batchIds
     * @return
     */
    @Override
    public boolean delBatchCourseComment(String batchIds) {
        try{
            String[] split = batchIds.split(",");
            List<CourseComment> collect = Arrays.stream(split).map(idsStr -> {
                CourseComment courseComment = new CourseComment();
                courseComment.setId(new Long(idsStr));
                return courseComment;
            }).collect(Collectors.toList());
            return this.removeByIds(collect);
        }catch (Exception exception){
            throw new PugValidatorException(ResultStatusEnum.SERVER_DB_ERROR);
        }
    }
}
